package com.liu.frame.base;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liu.frame.R;
import com.liu.frame.customview.TitleBar;
import com.liu.frame.utils.NetWorkUtil;
import com.liu.frame.utils.OtherUtils;
import com.liu.frame.utils.ToastUtils;


/**
 * Author:Created by 刘智强 on 2015/10/20.
 * Effect:fragment的基类
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    public Activity mActThis;
    protected TitleBar myTitleBar;//标题栏
    /**
     * 是否对用户可见
     */
    protected boolean mIsVisible;
    /**
     * 是否加载完成
     * 当执行完oncreatview,View的初始化方法后方法后即为true
     */
    protected boolean mIsPrepare;

    /**
     * 初始化控件
     */
    protected <T extends View> T $(int id) {
        return (T) mActThis.findViewById(id);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(getFragmentName(), " onAttach()");
        mActThis = activity;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(getFragmentName(), " onCreate()");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(getFragmentName(), " onCreateView()");
        mIsPrepare = true;

        if (getContentViewLayoutID() != 0) {
            return inflater.inflate(getContentViewLayoutID(), null);
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(getFragmentName(), " onViewCreated()");
        initData(getArguments());
        myTitleBar = $(R.id.my_titlebar);
        initView(view);
        initEvents();
        onLazyLoad();
        /**
         * 无网络去加载缓存
         */
        if (!NetWorkUtil.isNetwork(mActThis)) {
            ToastUtils.myToast(mActThis, "暂无网络");
            loadCache();
        } else {
            objectLogic();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(getFragmentName(), "onActivityCreated()");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.mIsVisible = isVisibleToUser;
        onFirstUserVisible();
        if (isVisibleToUser) {
            onVisibleToUser();
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        Log.i(getFragmentName(), " onStart()");
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.i(getFragmentName(), " onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(getFragmentName(), " onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(getFragmentName(), " onStop()");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(getFragmentName(), " onDestroyView()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(getFragmentName(), " onDestroy()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(getFragmentName(), " onDetach()");
    }

    /**
     * 初始化数据
     *
     * @param arguments 接收到的从其他地方传递过来的参数
     */
    protected void initData(Bundle arguments) {}

    /**
     * 用户可见时执行的操作
     */
    protected void onVisibleToUser() {
        if (mIsPrepare && mIsVisible) {
            onLazyLoad();
        }
    }

    protected void onLazyLoad() {
    }




    /**
     * 设置布局
     */
    protected abstract int getContentViewLayoutID();

    /**
     * 初始化组件
     *
     * @param v
     */
    public abstract void initView(View v);

    /**
     * 业务逻辑处理
     */

    public abstract void objectLogic();

    /**
     * 无网络去加载缓存
     */
    public abstract void loadCache();

    /**
     * 事件监听
     */
    public abstract void initEvents();

    /**
     * 第一次用户可见
     */
    protected abstract void onFirstUserVisible();


    /**
     * 处理监听
     *
     * @param v
     */
    public abstract void OnClickEvents(View v);

    @Override
    public void onClick(View v) {
        if (OtherUtils.fastClick()) {
            OnClickEvents(v);
        }

    }

    /**
     * fragment name
     */
    public abstract String getFragmentName();


}
